// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="T4TemplateResourceExtractor.cs" company="Microsoft.Blueprints.Contrib Team">
//   (C) Microsoft.Blueprints.Contrib Team 2009
// </copyright>
// <summary>
//   Defines the T4TemplateResourceExtractor type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Blueprints.Contrib.Tests.Utils
{
    #region Using Directives

    using System;
    using System.IO;
    using System.Reflection;

    #endregion

    /// <summary>
    /// Utility Class for extracing T4Template files from the currently executing assembly and
    /// writing them to a temporary file.
    /// </summary>
    public class T4TemplateResourceExtractor
    {
        /// <summary>
        /// Extracts the specified resource from the current assembly and writes
        /// it to a temporary file.
        /// </summary>
        /// <param name="resourceName">
        /// The resource name.
        /// </param>
        /// <returns>
        /// Path to the temp file.
        /// </returns>
        public static string CreateTemporaryFileFromResource(string resourceName)
        {
            string contents = GetResourceContents(resourceName);
            string localDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string appPath = Path.Combine(localDataPath, Assembly.GetExecutingAssembly().GetName().Name);
            string tempFilePath = Path.Combine(appPath, resourceName);

            if (!Directory.Exists(appPath))
            {
                Directory.CreateDirectory(appPath);
            }

            if (File.Exists(tempFilePath))
            {
                File.Delete(tempFilePath);
            }

            File.WriteAllText(tempFilePath, contents);

            return tempFilePath;
        }

        /// <summary>
        /// Extracts the contents of the specified resource from the currently executing assembly.
        /// </summary>
        /// <param name="resourceName">
        /// The resource name.
        /// </param>
        /// <returns>
        /// Contents of the specified resource.
        /// </returns>
        public static string GetResourceContents(string resourceName)
        {
            string result = string.Empty;

            if (resourceName != null)
            {
                Assembly assembly = Assembly.GetExecutingAssembly();

                if (assembly != null)
                {
                    using (TextReader textReader = new StreamReader(assembly.GetManifestResourceStream(resourceName)))
                    {
                        result = textReader.ReadToEnd();
                    }
                }
            }

            return result;
        }
    }
}